
#region Using Statements

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.GamerServices;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Media;
    using Microsoft.Xna.Framework.Net;
    using Microsoft.Xna.Framework.Storage;

#endregion

namespace HumanElement.Xna.Proof.TestGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class TestGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        private StageManager stage;

        private Action<Game> initializeDelegate;
        private Action<ContentManager> loadContentDelegate;
        private Action unloadContentDelegate;
        private Action<GameTime> updateDelegate;
        private Action<GraphicsDevice, GameTime> drawDelegate;

        /// <summary>
        /// Initializes a new instance of the <see cref="TestGame"/> class.
        /// </summary>
        /// <param name="initializeDelegate">The initialize delegate.</param>
        /// <param name="loadContentDelegate">The load content delegate.</param>
        /// <param name="unloadContentDelegate">The unload content delegate.</param>
        /// <param name="updateDelegate">The update delegate.</param>
        /// <param name="drawDelegate">The draw delegate.</param>
        public TestGame(Action<Game> initializeDelegate, Action<ContentManager> loadContentDelegate, Action unloadContentDelegate, Action<GameTime> updateDelegate, Action<GraphicsDevice, GameTime> drawDelegate) : this()
        {
            this.initializeDelegate = initializeDelegate;
            this.loadContentDelegate = loadContentDelegate;
            this.unloadContentDelegate = unloadContentDelegate;
            this.updateDelegate = updateDelegate;
            this.drawDelegate = drawDelegate;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TestGame"/> class.
        /// </summary>
        public TestGame() : base()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {         
            base.Initialize();
            OnInitialize(this);

            stage = new StageManager(this);
        }

        /// <summary>
        /// Called when [initialize].
        /// </summary>
        /// <param name="game">The game.</param>
        protected void OnInitialize(Game game)
        {
            if(initializeDelegate == null)
            {
                initializeDelegate(game);
            }
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            OnLoadContent(Content);
        }

        /// <summary>
        /// Called when [load content].
        /// </summary>
        /// <param name="content">The content.</param>
        protected void OnLoadContent(ContentManager content)
        {
            if(loadContentDelegate == null)
            {
                loadContentDelegate(content);
            }
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            OnUnloadContent();
        }

        /// <summary>
        /// Called when [unload content].
        /// </summary>
        protected void OnUnloadContent()
        {
            if (unloadContentDelegate == null)
            {
                unloadContentDelegate();
            }            
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            base.Update(gameTime);

            OnUpdate(gameTime);
        }

        /// <summary>
        /// Called when [update].
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        protected void OnUpdate(GameTime gameTime)
        {
            if(updateDelegate == null)
            {
                updateDelegate(gameTime);
            }
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            base.Draw(gameTime);

            OnDraw(GraphicsDevice, gameTime);
        }

        /// <summary>
        /// Called when [draw].
        /// </summary>
        /// <param name="graphics">The graphics.</param>
        /// <param name="gameTime">The game time.</param>
        protected void OnDraw(GraphicsDevice graphics, GameTime gameTime)
        {
            if(drawDelegate == null)
            {
                drawDelegate(graphics, gameTime);
            }
        }
    }
}
